﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Bird.Services.Events;
using Bird.Core.Domain.Projects;
using Bird.Core.Events;
using System.Collections.Generic;
namespace Bird.Test
{
    [TestClass]
    public class Events
    {
        class ProjectEventWatcher : IEventWatcher<EntityDeleted<Project>>
        {
            public List<Project> DeletedProjects { get; private set; }
            public ProjectEventWatcher()
            {
                DeletedProjects = new List<Project>();
            }

            public void HandleEvent(EntityDeleted<Project> message)
            {
                DeletedProjects.Add(message.Entity);
                Console.WriteLine("Handle EntityDeleted:{0}" ,message.Entity.ProjectTitle);
            }
        }


        ProjectEventWatcher projectEventWatcher;
        IEventPublisher eventPublisher;
        IEventSubcriptionService eventSubscriptionService;
        [TestInitialize]
        public void SetUp()
        {
            projectEventWatcher = new ProjectEventWatcher();
            eventSubscriptionService = new EventSubscriptionService();
            eventPublisher = new EventPublisher(eventSubscriptionService);
            eventSubscriptionService.AddEventWatcher(projectEventWatcher);
        }

        [TestMethod]
        public void TestDeleteProject()
        {
            Project p = new Project()
              {
                  ProjectTitle = "test deleted",
                  CreateTime = DateTime.Now
              };
            eventPublisher.EntityDeleted(p);
            Assert.IsTrue(projectEventWatcher.DeletedProjects.Contains(p));
        }

    }
}
